<template>
  <div class="app-container">
    <div class="crud-group">
      <el-input
        v-model="paramsForm.taskName"
        type="text"
        placeholder="请输入任务名"
        clearable
      />
      <el-input
        v-model="paramsForm.sourceDbTableName"
        type="text"
        placeholder="源库表名"
        clearable
      />
      <el-input
        v-model="paramsForm.targetDbTableName"
        type="text"
        placeholder="目标库表名"
        clearable
      />
      <el-select
        v-model="paramsForm.flag"
        placeholder="标识"
        clearable
      >
        <el-option
          v-for="item of ['WG','JQ']"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-select
        v-model="paramsForm.taskState"
        placeholder="状态"
        clearable
      >
        <el-option
          v-for="item in stateOption"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-date-picker
        v-model="paramsForm.timeRange"
        type="datetimerange"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        :default-time="['00:00:00','23:59:59']"
        value-format="yyyy-MM-dd HH:mm:ss"
        clearable
        @change="(val)=>{
          if(!val){
            this.paramsForm.timeRange = []
          }
        }"
      />

      <el-button
        type="success"
        icon="el-icon-search"
        @click="getData"
      >
        搜索
      </el-button>
      <el-button
        type="warning"
        icon="el-icon-refresh-left"
        @click="resetForm"
      >
        重置
      </el-button>
      <el-dropdown
        style="margin-left: 0.6rem;"
        :disabled="!selection"
        @command="handleCommand"
      >
        <el-button
          type="primary"
          :disabled="!selection"
        >
          更多操作<i class="el-icon-arrow-down el-icon--right" />
        </el-button>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item
            command="reRun"
          >重跑统计</el-dropdown-item>
          <el-dropdown-item
            command="pause"
          >暂停统计</el-dropdown-item>
          <el-dropdown-item
            command="continue"
          >继续统计</el-dropdown-item>

          <el-dropdown-item
            command="reRunFA"
          >重跑稽核</el-dropdown-item>
          <el-dropdown-item
            command="pauseFA"
          >暂停稽核</el-dropdown-item>
          <el-dropdown-item
            command="continueFA"
          >继续稽核</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>

    <el-table
      ref="table"
      v-loading="isLoading"
      :data="data"
      border
      @selection-change="handleSelectionChange"
      @select-all="()=>{
        if(selection){
          this.$refs.table.clearSelection()
        }
      }"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        label="任务名称"
        prop="taskName"
        sortable
        fixed
        min-width="100px"
      />
      <el-table-column
        label="标识"
        prop="flag"
        sortable
        fixed
        min-width="100px"
      />
      <el-table-column
        label="库名"
        prop="dbName"
        sortable
      />
      <el-table-column
        label="源表名信息"
        sortable
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="()=>{
              selectedName = scope.row.sourceDbTableName
              dialogVisible = true
            }"
          >
            查看详情
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="目标表名信息"
        sortable
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="()=>{
              selectedName = scope.row.targetDbTableName
              dialogVisible = true
            }"
          >
            查看详情
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="源表数量"
        prop="sourceHdfsNumber"
        sortable
        show-overflow-tooltip
      />
      <el-table-column
        label="目标表数量"
        prop="targetHdfsNumber"
        sortable
        show-overflow-tooltip
      />
      <el-table-column
        label="源总数"
        prop="sourceCountNumber"
        sortable
        show-overflow-tooltip
      />
      <el-table-column
        label="目标总数"
        prop="targetCountNumber"
        sortable
        show-overflow-tooltip
      />

      <el-table-column
        label="状态"
        prop="taskState"
        sortable
        min-width="80px"
      >
        <template slot-scope="scope">
          <el-tag
            :type="getStateColor(scope.row.taskState)"
          >
            {{ scope.row.taskState }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="源开始时间"
        prop="sourceStandbyTimeSta"
        sortable
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>
            {{ convertDate(scope.row.sourceStandbyTimeSta) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="源结束时间"
        prop="sourceStandbyTimeEnd"
        sortable
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>
            {{ convertDate(scope.row.sourceStandbyTimeEnd) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="目标开始时间"
        prop="targetStandbyTimeSta"
        sortable
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>
            {{ convertDate(scope.row.targetStandbyTimeSta) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="目标结束时间"
        prop="targetStandbyTimeEnd"
        sortable
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>
            {{ convertDate(scope.row.targetStandbyTimeEnd) }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        min-width="120"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="() => {
              selectedTaskId = scope.row.id
              statisticsDetailsDialogVisible = true
            }
            "
          >
            统计记录
          </el-button>
          <el-button
            type="text"
            @click="() => {
              selectedTaskId = scope.row.id
              selectedFlag = scope.row.flag
              fullAuditDialogVisible = true
            }
            "
          >
            稽核明细
          </el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="结束时间"
        prop="lastUpdateTime"
        sortable
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ (scope.row.taskState && scope.row.taskState === '进行中') ? '' : scope.row.lastUpdateTime }}</span>
        </template>
      </el-table-column> -->

    </el-table>
    <div class="pagination-container">
      <el-pagination
        :current-page="paramsForm.current"
        :page-size="paramsForm.size"
        layout="prev, pager, next,sizes,total"
        :total="total"
        @size-change="(val)=>{
          paramsForm.current = 1
          paramsForm.size = val
          getData()
        }"
        @current-change="(val)=>{
          paramsForm.current = val
          getData(false)
        }"
      />
    </div>

    <TableNameList
      :dialog-visible="dialogVisible"
      :table-name-list="selectedName"
      @close="()=>{
        dialogVisible = false
      }"
    />

    <ComparisonDialog
      :dialog-visible="fullAuditDialogVisible"
      :task-id="selectedTaskId"
      :flag="selectedFlag"
      @close="()=>{
        fullAuditDialogVisible = false
      }"
    />

    <StatisticsDetailsDialog
      :dialog-visible="statisticsDetailsDialogVisible"
      :task-id="selectedTaskId"
      @close="()=>{
        statisticsDetailsDialogVisible = false
      }"
    />

    <ReRunDialog
      :dialog-visible="reRunDialogVisible"
      :selection="selection"
      :type="type"
      @close="()=>{
        reRunDialogVisible = false
        getData()
      }"
    />
    <ContinueDialog
      :dialog-visible="continueDialogVisible"
      :selection="selection"
      :type="type"
      @close="()=>{
        continueDialogVisible = false
        getData()
      }"
    />
  </div>
</template>

<script>
import cbApi from '@/api/coldStandby/index'
import dateUtil from '@/utils/date'
import TableNameList from './components/TableNameListDialog.vue'
import ComparisonDialog from './components/FullAuditDialog.vue'
import StatisticsDetailsDialog from './components/StatisticsDetailsDialog.vue'
import ReRunDialog from './components/ReRunDialog.vue'
import ContinueDialog from './components/ContinueDialog.vue'

const form = {
  sourceDbTableName: '',
  targetDbTableName: '',
  taskName: '',
  current: 1,
  size: 20,
  flag: '',
  taskState: '',
  timeRange: [
    dateUtil.getDateStr(new Date(new Date().setHours(0, 0, 0, 0))),
    dateUtil.getDateStr(new Date(new Date().setHours(0, 0, 0, 0) + 24 * 60 * 60 * 1000 - 1))
  ]
}

export default {
  components: {TableNameList, ComparisonDialog, StatisticsDetailsDialog, ReRunDialog, ContinueDialog},
  data() {
    return {
      data: [],
      total: 0,
      isLoading: false,
      paramsForm: {...form},
      dialogVisible: false,
      fullAuditDialogVisible: false,
      statisticsDetailsDialogVisible: false,
      reRunDialogVisible: false,
      continueDialogVisible: false,
      selectedTaskId: '',
      selectedFlag: '',
      selectedName: '',
      selection: null,
      type: '统计',
      stateOption: [
        {
          label: '待统计',
          value: '待统计'
        },
        {
          label: '统计成功',
          value: '统计成功'
        },
        {
          label: '统计失败',
          value: '统计失败'
        },
        {
          label: '稽核失败',
          value: '稽核失败'
        },
        {
          label: '稽核成功',
          value: '稽核成功'
        }
      ]
    }
  },
  created() {
    this.getData()
  },

  methods: {
    convertDate(timeStamp) {
      if (!timeStamp) { return '' }
      return dateUtil.getDateStr(new Date(timeStamp))
    },
    getStateColor(state) {
      if (state === '统计成功' || state === '稽核成功') {
        return 'success'
      } else if (state === '稽核失败' || state === '稽核失败') {
        return 'danger'
      } else if (state === '待统计') {
        return ''
      } else {
        return 'warning'
      }
    },
    getData(resetPage = true) {
      this.isLoading = true
      if (resetPage) {
        this.paramsForm.current = 1
      }
      const param = {
        staDate: this.paramsForm.timeRange ? this.paramsForm.timeRange[0] : '',
        endDate: this.paramsForm.timeRange ? this.paramsForm.timeRange[1] : '',
        ...this.paramsForm
      }

      cbApi.cbCompAuditTask(param)
        .then((res) => {
          this.data = res.records
          this.total = res.total
        })
        .finally(() => {
          this.isLoading = false
        })
    },
    reRun() {
      if (this.type === '统计') {
        this.reRunDialogVisible = true
        return
      } else {
        const form = {
          flag: this.selection.flag,
          order: this.selection.taskName.includes('【LT】') ? '' : 'QL',
          mark: this.selection.mark,
          dbName: this.selection.dbName
        }
        cbApi.reRunFullAudit(form)
        setTimeout(() => {
          this.$message.success('开始重跑')
          this.getData()
        }, 500)
      }
    },
    async pause() {
      this.type === '统计' ? await cbApi.pauseCbParamConfig() : await cbApi.pauseFullAudit()
      this.$message.success('暂停成功')
      this.getData()
    },
    continue() {
      if (this.type === '统计') {
        this.continueDialogVisible = true
        return
      }
      const form = {
        flag: this.selection.flag,
        order: this.selection.taskName.includes('【LT】') ? '' : 'QL',
        mark: this.selection.mark,
        dbName: this.selection.dbName,
        tableName: this.selection.tableName
      }
      cbApi.continueFullAudit(form)
      setTimeout(() => {
        this.$message.success('继续重跑')
        this.getData()
      }, 500)
    },
    handleCommand(command) {
      if (command.includes('FA')) {
        this.type = '稽核'
        this[command.replace('FA', '')]()
        return
      } else {
        this.type = '统计'
      }
      this[command]()
    },
    resetForm() {
      this.paramsForm = {...form}
      this.getData()
    },
    handleSelectionChange(val) {
      if (val.length > 1) {
        this.$refs.table.clearSelection()
        this.$refs.table.toggleRowSelection(val[val.length - 1])
      }
      this.selection = val[val.length - 1]
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
